home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / java_Win / demo / GraphLayout / Graph.class (.txt) < prev    next >
Encoding:
Java Class File  |  1995-10-12  |  3.4 KB  |  102 lines

  1. import java.applet.Applet;
  2. import java.awt.BorderLayout;
  3. import java.awt.Button;
  4. import java.awt.Checkbox;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Dimension;
  8. import java.awt.Event;
  9. import java.awt.Panel;
  10. import java.util.StringTokenizer;
  11.  
  12. public class Graph extends Applet {
  13.    GraphPanel panel;
  14.  
  15.    public void init() {
  16.       ((Container)this).setLayout(new BorderLayout());
  17.       this.panel = new GraphPanel(this);
  18.       ((Container)this).add("Center", this.panel);
  19.       Panel p = new Panel();
  20.       ((Container)this).add("South", p);
  21.       ((Container)p).add(new Button("Scramble"));
  22.       ((Container)p).add(new Button("Shake"));
  23.       ((Container)p).add(new Checkbox("Stress"));
  24.       ((Container)p).add(new Checkbox("Random"));
  25.       String edges = ((Applet)this).getParameter("edges");
  26.       StringTokenizer t = new StringTokenizer(edges, ",");
  27.  
  28.       while(t.hasMoreTokens()) {
  29.          String str = t.nextToken();
  30.          int i = str.indexOf(45);
  31.          if (i > 0) {
  32.             int len = 50;
  33.             int j = str.indexOf(47);
  34.             if (j > 0) {
  35.                len = Integer.valueOf(str.substring(j + 1));
  36.                str = str.substring(0, j);
  37.             }
  38.  
  39.             this.panel.addEdge(str.substring(0, i), str.substring(i + 1), len);
  40.          }
  41.       }
  42.  
  43.       Dimension d = ((Component)this).size();
  44.       String center = ((Applet)this).getParameter("center");
  45.       if (center != null) {
  46.          Node n = this.panel.nodes[this.panel.findNode(center)];
  47.          n.x = (double)(d.width / 2);
  48.          n.y = (double)(d.height / 2);
  49.          n.fixed = true;
  50.       }
  51.  
  52.    }
  53.  
  54.    public void start() {
  55.       this.panel.start();
  56.    }
  57.  
  58.    public void stop() {
  59.       this.panel.stop();
  60.    }
  61.  
  62.    public boolean action(Event evt, Object arg) {
  63.       if (arg instanceof Boolean) {
  64.          if (((Checkbox)evt.target).getLabel().equals("Stress")) {
  65.             this.panel.stress = (Boolean)arg;
  66.          } else {
  67.             this.panel.random = (Boolean)arg;
  68.          }
  69.  
  70.          return true;
  71.       } else if ("Scramble".equals(arg)) {
  72.          ((Applet)this).play(((Applet)this).getCodeBase(), "audio/computer.au");
  73.          Dimension d = ((Component)this).size();
  74.  
  75.          for(int i = 0; i < this.panel.nnodes; ++i) {
  76.             Node n = this.panel.nodes[i];
  77.             if (!n.fixed) {
  78.                n.x = (double)10.0F + (double)(d.width - 20) * Math.random();
  79.                n.y = (double)10.0F + (double)(d.height - 20) * Math.random();
  80.             }
  81.          }
  82.  
  83.          return true;
  84.       } else if ("Shake".equals(arg)) {
  85.          ((Applet)this).play(((Applet)this).getCodeBase(), "audio/gong.au");
  86.          ((Component)this).size();
  87.  
  88.          for(int i = 0; i < this.panel.nnodes; ++i) {
  89.             Node n = this.panel.nodes[i];
  90.             if (!n.fixed) {
  91.                n.x += (double)80.0F * Math.random() - (double)40.0F;
  92.                n.y += (double)80.0F * Math.random() - (double)40.0F;
  93.             }
  94.          }
  95.  
  96.          return true;
  97.       } else {
  98.          return false;
  99.       }
  100.    }
  101. }
  102.